﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace System.Peppers.Wmi
{
    /// <summary>
    /// The Win32_Trustee abstract WMI class specifies a trustee that can be a name or a security identifier (SID) byte array.
    /// </summary>
    public sealed class WmiTrustee : WmiWrapper
    {
        #region Static
        private static class WmiProperties
        {
            public const string Domain = "Domain",
                Name = "Name",
                SID = "SID",
                SidLength = "SidLength",
                SIDString = "SIDString";
        }
        #endregion

        /// <summary>
        /// Creates a new instance
        /// </summary>
        public WmiTrustee()
            : base(new ManagementObject(WmiClasses.Win32_Trustee))
        { }

        /// <summary>
        /// Domain to which a trustee belongs.
        /// </summary>
        public string Domain
        {
            get { return Convert.ToString(this[WmiProperties.Domain]); }
            set { this[WmiProperties.Domain] = value; }
        }

        /// <summary>
        /// A trustee can be a user account, group account, or logon session.
        /// </summary>
        public string Name
        {
            get { return Convert.ToString(this[WmiProperties.Name]); }
            set { this[WmiProperties.Name] = value; }
        }

        /// <summary>
        /// SID that uniquely identifies a user or group.
        /// </summary>
        public byte[] SID
        {
            get { return this[WmiProperties.SID] as byte[]; }
            set { this[WmiProperties.SID] = value; }
        }

        /// <summary>
        /// Length of a SID—in bytes.
        /// </summary>
        public uint SidLength
        {
            get { return Convert.ToUInt32(this[WmiProperties.SidLength] ?? 0); }
            set { this[WmiProperties.SidLength] = value; }
        }

        /// <summary>
        /// SID of a trustee in string format. The format for a string value is the following:
        /// 1. The "S" character identifies the series of digits as a SID.
        /// 2. The revision level.
        /// 3. Identifier authority value.
        /// 4. One or more subauthority values.
        ///     Example: "S-1-1-0"
        /// </summary>
        public string SIDString
        {
            get { return Convert.ToString(this[WmiProperties.SIDString]); }
            set { this[WmiProperties.SIDString] = value; }
        }
    }
}
